package com.situ.vivamarket.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.situ.vivamarket.intcp.AdminLoginInterceptor;
import com.situ.vivamarket.intcp.FrontInterceptor;
import com.situ.vivamarket.intcp.MemberLoginInterceptor;
import com.situ.vivamarket.service.front.CartItemService;
import com.situ.vivamarket.service.OrderService;
import com.situ.vivamarket.service.front.WishlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.lang.reflect.Method;
import java.util.Arrays;

@Configuration
@EnableCaching
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true) //启动代理，暴露代理,表示Cglib动态代理
@EnableTransactionManagement  //启用事务
public class MyConfig implements WebMvcConfigurer {
    private OrderService orderService;
    private CartItemService cartItemService;
    private WishlistService wishlistService;
    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }
    @Autowired
    public void setCartItemService(CartItemService cartItemService) {
        this.cartItemService = cartItemService;
    }
    @Autowired
    public void setWishlistService(WishlistService wishlistService) {
        this.wishlistService = wishlistService;
    }

    //    支付宝沙箱
    @Value("${alipay.app-id}")
    private String appId;

    @Value("${alipay.merchant-private-key}")
    private String merchantPrivateKey;

    @Value("${alipay.alipay-public-key}")
    private String alipayPublicKey;

    @Value("${alipay.gateway-url}")
    private String gatewayUrl;


    //    在SpingMvc中添加拦截器

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new AdminLoginInterceptor())
    //                后端管理员认证拦截器
                .addPathPatterns("/vm/sys/**/*") // 设定拦截模式
                .excludePathPatterns("/vm/sys/login/**","/vm/sys/register/**", "/vm/sys/captcha/**"); //设定放行路径
    //        前端会员认证拦截器
        registry.addInterceptor(new MemberLoginInterceptor())
                .addPathPatterns("/vm/front/shop/**/*", "/vm/front/account/**",
                        "/front/api/v1/shop/**/*");
//        检测有没有登录的拦截器，用于头部显示用户信息
        registry.addInterceptor(new FrontInterceptor(this.orderService,this.cartItemService,this.wishlistService))
                .addPathPatterns("/vm/front/**/*");
    }

    @Bean //让Spring创建返回值的唯一实例（单例）
    public KeyGenerator myKeyGenerator() {
        return new KeyGenerator() {
            @Override
            public Object generate(Object target, Method method, Object... params) {
                return method.getName() + Arrays.toString(params);
            }
        };
    }
    //创建一个AlipayClient实例
    @Bean
    public AlipayClient alipayClient() {
        return new DefaultAlipayClient(gatewayUrl,
                appId, merchantPrivateKey, "json", "UTF-8",
                alipayPublicKey, "RSA2");//sign-type也可以在配置类里写，但是名比值还长，算了
    }

}
